import {
	requestPayment
} from '@/utils/app'
export default {
	data() {
		return {
			tabs: [{
				name: '待付款',
				id: 0
			}, {
				name: '待发货',
				id: 1
			}, {
				name: '待收货',
				id: 2
			}, {
				name: '已完成',
				id: 3
			}],
			current: 0,
			limit: 10,
			page: 1,
			orderList: []
		};
	},
	onLoad(options) {
		this.current = options.type ? Number(options.type) : 0

		uni.$on('updateOrder', res => {
			this.$refs.paging.reload()
		})
	},
	methods: {
		// 获取订单列表
		//0未支付  1已支付(待发货) 2已发货 3已完成
		async getOrderList(pageNo, pageSize) {
			var res = await this.$get('server/index/getgoodsorderlist', {
				status: this.tabs[this.current].id,
				page: pageNo,
				limit: pageSize
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				this.$refs.paging.complete(false);
				return;
			}
			this.$refs.paging.complete(res.list);
		},
		changeTab(index) {
			this.current = index
			this.$refs.paging.reload()
		},
		// 跳转到订单详情
		onClick(item) {
			this.$navTo('pages/mine/order-detail/order-detail', {
				orderId: item.id
			})
		},
		// 确认收货
		onShouhuo(item) {
			this.$showModal({
				title: '提示',
				content: '是否确认收货？',
				success: (res) => {
					if (res.confirm) {
						console.log('用户点击确定')
						this.shouhuoApi(item)
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		},
		async shouhuoApi(item) {
			var res = await this.$get('server/index/shouhuo', {
				id: item.id,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('确认收货成功')
			setTimeout(() => {
				this.$refs.paging.reload()
			}, 500)
		},
		// 取消订单
		onCancel(item) {
			this.$showModal({
				title: '提示',
				content: '确认取消订单？',
				success: (res) => {
					if (res.confirm) {
						console.log('用户点击确定')
						this.cancelApi(item)
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		},
		async cancelApi(item) {
			var res = await this.$get('server/index/cancelorder', {
				id: item.id,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('取消成功')
			setTimeout(() => {
				this.$refs.paging.reload()
			}, 500)
		},
		// 去付款
		async onPay(item) {
			var res = await this.$get('server/index/topay', {
				id: item.id,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			requestPayment(res, 1)
				.then(() => {
					this.$toast('支付成功')
					setTimeout(() => {
						uni.navigateBack()
						uni.$emit('updateOrder')
					}, 500)
				})
				.catch(err => this.$toast('订单未支付'))
				.finally(() => {
					this.$toast('取消支付')
				})
		}
	}
}
